package com.android.bonn.fast

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.android.bonn.fast.adapter.SimpleCellDelegateAdapter
import com.android.bonn.fast.adapter.ext.create
import com.android.bonn.fast.databinding.ActivityAnyAdapterMainBinding
import java.util.concurrent.atomic.AtomicInteger

class AnyAdapterListActivity : AppCompatActivity() {

  private lateinit var mBinding: ActivityAnyAdapterMainBinding
  private lateinit var adapter: SimpleCellDelegateAdapter

  data class FormData(
    var title: String = "",
    var number: String = "",
    var phone: String = "",
    var address: String = "",
    var customCat: String = ""
  )


  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    mBinding = ActivityAnyAdapterMainBinding.inflate(layoutInflater)
    setContentView(mBinding.root)

    val data = FormData("")

    adapter = SimpleCellDelegateAdapter.Builder()
      .create(R.layout.item_view_layout2, { FormCellView(it, data) }, { _, item -> item is FromView })
      //.create(R.layout.item_view_layout2, { JavaCellView(it, data) }, { _, item -> item is InnerFromView })
      .build()

    mBinding.testListView.adapter = adapter
    adapter.submitList(getTestData2())
    adapter.notifyDataSetChanged()

    mBinding.btnSubmit.setOnClickListener {
      Log.i("sadad", "===============${data}")
    }

    val count = AtomicInteger()
    mBinding.btnChange.setOnClickListener {
      adapter.clear()
      if (count.incrementAndGet() % 2 != 0) {
        adapter.submitList(getTestData2())
      } else {
        adapter.submitList(getTestData())
      }
      adapter.notifyDataSetChanged()
    }

    mBinding.btnRefresh.setOnClickListener {
      data.title = "666666666"
      adapter.notifyItemChanged(0)
    }
  }


  private fun getTestData() = mutableListOf(
    FromView("1"),
    FromView("1", "编号", "自动编号"),
    FromView("1", "联系电话", "请输入"),
    FromView("1", "收货地址", "请输入"),
    InnerFromView("5", "客户分类", "请选择"),
    InnerFromView("5", "客户分类", "请选择"),
    InnerFromView("5", "客户分类", "请选择"),
    InnerFromView("5", "客户分类", "请选择"),
    InnerFromView("5", "客户分类", "请选择"),
    InnerFromView("5", "客户分类", "请选择"),
    InnerFromView("5", "客户分类", "请选择"),
    InnerFromView("5", "客户分类", "请选择"),
    InnerFromView("5", "客户分类", "请选择"),
    InnerFromView("5", "客户分类", "请选择"),
    InnerFromView("5", "客户分类", "请选择"),
    InnerFromView("5", "客户分类", "请选择"),
    InnerFromView("5", "客户分类", "请选择"),
    InnerFromView("5", "客户分类", "请选择"),
    InnerFromView("5", "客户分类", "请选择"),
    InnerFromView("5", "客户分类", "请选择"),
    InnerFromView("5", "客户分类", "请选择"),
    InnerFromView("5", "客户分类", "请选择"),
    InnerFromView("5", "客户分类", "请选择"),
    InnerFromView("5", "客户分类", "请选择"),
    InnerFromView("5", "客户分类", "请选择"),
    InnerFromView("5", "客户分类", "请选择"),
    InnerFromView("5", "客户分类", "请选择"),
    InnerFromView("5", "客户分类", "请选择"),
    InnerFromView("5", "客户分类", "请选择"),
    InnerFromView("5", "客户分类", "请选择"),
    InnerFromView("5", "客户分类", "请选择"),
    InnerFromView("5", "客户分类", "请选择"),
    InnerFromView("5", "客户分类", "请选择"),
    InnerFromView("5", "客户分类", "请选择"),
    InnerFromView("5", "客户分类", "请选择"),
    InnerFromView("5", "客户分类", "请选择"),
    InnerFromView("5", "客户分类", "请选择"),
    InnerFromView("5", "客户分类", "请选择")
  )


  private fun getTestData2() = buildList {
    for (index in 0..100) {
      add(FromView("$index", "编号", "自动编号"))
    }
  }
}